package Dao.Impl;

import Dao.LikeDAO;
import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Su
 * @Date 2025/4/29 10:42
 * @Version 1.0 （版本号）
 */
public class LikeDAOImpl implements LikeDAO {
    private Connection connection;

    public LikeDAOImpl(Connection connection) {
        this.connection = connection;
    }
    @Override
    public boolean hasLiked(int articleId, int userId) throws SQLException {
        String sql =  "SELECT status FROM article_likes WHERE article_id = ? AND user_id = ?";
        try(PreparedStatement pstmt = connection.prepareStatement(sql)) {
            connection = DBUtil.getConnection();
            pstmt.setInt(1, articleId);
            pstmt.setInt(2, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                return rs.next() && rs.getInt("status") == 1;
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
    }
}
